import { Modal } from "ant-design-vue"
import { h } from "vue"
import { dataTypes } from "@/utils/index"
import type { deleteInterface } from "./interface"
const confirmModel = (params: deleteInterface = {}) => {
  Modal.confirm({
    title: params.title || "确认删除？",
    icon: h("i", {
      class: `iconfont icon-warning color-warning`,
      style: "line-height: 1; font-size: 20px;",
    }),
    width: params.width || "480px",
    closable: true,
    class: "confirm-model-tip",
    content: params.content || "是否确认删除该数据",
    okText: params.okText || "确定",
    cancelText: params.cancelText || "取消",
    async onOk() {
      const isFunction = dataTypes().isFunction(params.okFunction)
      if (!isFunction) {
        console.log("不是函数")
        return
      }
      try {
        return await new Promise<void>((resolve, reject) => {
          params
            .okFunction?.()
            .then(() => {
              resolve()
            })
            .catch(() => {
              reject()
            })
        })
      } catch {
        return console.log("Oops errors!")
      }
    },
    onCancel() {
      const isFunction = dataTypes().isFunction(params.cancelFunction)
      if (!isFunction) {
        console.log("不是函数")
        return
      }
      params.cancelFunction?.()
    },
  })
}

export default confirmModel
